/**
 * 分析工具管理初始化
 */
var CdcTool = {
    id: "CdcToolTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CdcTool.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '代码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '描述', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '服务地址', field: 'serverIp', align: 'center', valign: 'middle', sortable: true},
        {title: '服务端口', field: 'serverPort', align: 'center', valign: 'middle', sortable: true},
        {title: '服务路径', field: 'serverPath', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'note', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:CdcTool.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    		_html += '<a href="javascript:CdcTool.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
    		return _html;
        }}];
};

/**
 * 检查是否选中
 */
CdcTool.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CdcTool.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增分析工具
 */
CdcTool.add = function () {
    var index = Sys.open('新增分析工具',Sys.backendPath + '/cdc/cdctool/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看分析工具详情
 */
CdcTool.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('分析工具详情',Sys.backendPath + '/cdc/cdctool/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除分析工具
 */
CdcTool.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/cdctool/delete", function (data) {
	            Sys.success("删除成功!");
	            CdcTool.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("cdcToolId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除分析工具?",operation);
    }
};

/**
 * 查询分析工具列表
 */
CdcTool.search = function () {
    var queryData = {};
    queryData['description'] = $("#description").val();
    queryData['serverIp'] = $("#serverIp").val();
    queryData['serverPort'] = $("#serverPort").val();
    queryData['serverPath'] = $("#serverPath").val();
    queryData['note'] = $("#note").val();
    queryData['isActive'] = $("#isActive").val();
    CdcTool.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = CdcTool.initColumn();
    var table = new BSTable(CdcTool.id, Sys.backendPath + "/cdc/cdctool/list", defaultColunms);
    table.setPaginationType("server");
    CdcTool.table = table.init();
});
